//
//  MomentarySliderView.m
//  Podcopter
//
//  Created by Aaron Thompson on 2/22/10.
//  Copyright 2010 Vanderbilt University. All rights reserved.
//

#import "MomentarySliderView.h"

@interface MomentarySliderView (private)

- (void)handleTouches:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)beginDecayingValue;
- (void)cancelDecayingValue;

@end

@implementation MomentarySliderView

@synthesize value;
@synthesize minValue;
@synthesize maxValue;
@synthesize decaySpeed;
@synthesize delegate;


- (id)initWithCoder:(NSCoder *)coder
{
	if ((self = [super initWithCoder:coder])) {
		// Set default values
		value = 0.0f;
		minValue = 0.0f;
		maxValue = 1.0f;
		decaySpeed = 2.0/30.0;
	}

	return self;
}

- (void)setValue:(float)newValue {
	value = newValue;
	
	if (delegate) {
		[delegate momentarySlider:self valueChanged:value];
	}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[self handleTouches:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	[self handleTouches:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	[self beginDecayingValue];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	[self beginDecayingValue];
}

- (void)handleTouches:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [[event touchesForView:self] anyObject];
	if (!touch) {
		return;
	}
	
	[self cancelDecayingValue];
	
	CGPoint point = [touch locationInView:self];
	self.value = (self.bounds.size.height-point.y) / self.bounds.size.height * (maxValue-minValue) + minValue;
	
	[self setNeedsDisplay];
}

- (void)beginDecayingValue
{
	// Not yet finished, we need to make this slide down gradually
	/*
    decayTimer = [[NSTimer alloc] initWithFireDate:[NSDate date]
										  interval:1.0/30.0
											target:self
										  selector:@selector(decayValue:)
										  userInfo:nil
										   repeats:YES];
	

	[[NSRunLoop currentRunLoop] addTimer:decayTimer forMode:NSDefaultRunLoopMode];
    */
}

- (void)decayValue:(id)object
{
	if (fabsf(value - defaultValue) < 0.02f || value < 0) {
		value = defaultValue;
		[self cancelDecayingValue];
	} else if (value > defaultValue) {
		value -= decaySpeed;
	} else {
		value += decaySpeed;
	}
	
	[self setNeedsDisplay];
}

- (void)cancelDecayingValue
{
	// Cancel out any timer
//	[decayTimer invalidate];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
	CGContextRef ctxt = UIGraphicsGetCurrentContext();
	
	// Fill the background with white
	CGContextSetGrayFillColor(ctxt, 1.0, 1.0);
	CGContextFillRect(ctxt, rect);

	CGRect levelRect = self.bounds;
	levelRect.size.height = value * self.bounds.size.height;
	levelRect.origin.y = rect.size.height - levelRect.size.height;

	// Draw the power level rectangle
	CGContextSetRGBFillColor(ctxt, 1.0f, 0.0f, 0.0f, 1.0f);
	CGContextFillRect(ctxt, levelRect);
}


- (void)dealloc {
	[super dealloc];
}


@end
